<?php

class User {

	private $db;

	public function __construct($db) {
		if (is_object($db)) {
			$this->db = $db;
		} else {
			die('Error! Authority construct need D3sql Object! Demo:  new Authority(- Add D3Sql() Object -)');
		}
	}

	/**
	* get User List and Detail.
	* 
	* @param mixed $userLogin
	*/
	public function getUser($userLogin = false) {
		$getUserSQLText = "
								SELECT * FROM e_shop_admin u  LEFT JOIN e_authority_group ag ON u.authority_group_id = ag.id   
							  ";
		if ($userLogin) {
			$getUserSQLText .= " WHERE u.login = '" . $userLogin . "' ";
		}

		$getUserSQLText .= " ORDER BY u.login ASC";

		$getUserSQL = $this->db->sql($getUserSQLText);
		$getUserArray = array();
		while ($getUser = $this->db->getir($getUserSQL)) {
			$getUserArray[] = $getUser;
		}

		return $getUserArray;
	}
	
	/**
	* get Member List
	* 
	* @param mixed $start
	* @param mixed $limit
	* @param mixed $userId
	* @param mixed $where
	*/
	public function getMember($start=0,$limit=20,$userId=false,$where=false) {
		$getUserSQLText = " SELECT * FROM e_user ";
		if ($userId) {
			$getUserSQLText .= " WHERE user_numid=".$userId;
		}
		if($where) {
			$getUserSQLText .= $where;
		}

		$getUserSQLText .= " ORDER BY user_id ASC LIMIT ".$start.",".$limit;

		$getUserSQL = $this->db->sql($getUserSQLText);
		$getUserArray = array();
		while ($getUser = $this->db->getir($getUserSQL)) {
			$getUserArray[] = $getUser;
		}

		return $getUserArray;
	}
	
	/**
	* count Member by Where
	* 
	* @param mixed $where
	*/
	public function countMember($where="") {
		$getUserSQLText = " SELECT COUNT(user_id) FROM e_user ";
		if ($where!= "") {
			$getUserSQLText .= " WHERE ".$where;
		}
		
		$getUserSQL = $this->db->say($getUserSQLText);
		print_r($getUserSQL);

		//return $getUserSQL;
	}
	
	
	/**
	* create New Admin User
	* 
	* @param mixed $shopID
	* @param mixed $userName
	* @param mixed $password
	* @param mixed $active
	* @param mixed $authID
	*/
	public function createUser($shopID,$userName,$password,$active,$authID)
	{
		$checkUserSQLText = " SELECT count(*) as toplam FROM e_shop_admin WHERE login = '".$userName."' ";
		$checkUserSQL = $this->db->sql($checkUserSQLText);
		$checkUser = $this->db->getir($checkUserSQL);
		
		$sonuc = array('sonuc','aciklama');
		
		if($checkUser['toplam'] == 0)
		{
			$insertText = " INSERT INTO e_shop_admin (shop_id,login, password, active, authority_group_id) VALUES (".$shopID.",'".$userName."','".md5($password)."',".$active.",".$authID.") ";
			if($this->db->sql($insertText))
			{
				$sonuc['sonuc'] = true;
				$sonuc['aciklama'] = 'Kullanıcı oluşturuldu.';
			}
			else
			{
				$sonuc['sonuc'] = false;
				$sonuc['aciklama'] = 'HATA! Sorgulama Hatası:'.$db->sonhata;
			}
		}
		else
		{
			$sonuc['sonuc'] = false;
			$sonuc['aciklama'] = 'Kullanıcı Adı Kullanılıyor.';
		}
		
		return $sonuc;
	}
	
	/**
	* get Wait Activation Member List
	* 
	* @param mixed $start
	* @param mixed $limit
	* @param mixed $where
	*/
	public function getWaitActivationMember($start=0,$limit=20,$id=false,$where=false)
	{
		$getUserSQLText = " SELECT * FROM e_user_temp ";
	   
		if($where) {
			$getUserSQLText .= $where;
		}

		$getUserSQLText .= " ORDER BY user_id ASC LIMIT ".$start.",".$limit;

		$getUserSQL = $this->db->sql($getUserSQLText);
		$getUserArray = array();
		while ($getUser = $this->db->getir($getUserSQL)) {
			$getUserArray[] = $getUser;
		}

		return $getUserArray;
	}
	
	
	

}

?>
